<?php
class FollowService extends BaseDbService
{
	public function __construct()
	{
		$this->dao	= new BaseDao('follow');
	}
	
	public function IsFollow($user_id,$follow_id)
	{
		return $this->fetch(array('user_id'=>'eq:'.$user_id,'follow_id'=>'eq:'.$follow_id));
	}
	
	public function FollowUser($user_id,$follow_id)
	{
		if ($user_id == $follow_id)
		{
			return false;
		}
		$followInfo	= $this->IsFollow($user_id, $follow_id);
		if ($followInfo)
		{
			return false;
		}
		$map = array('follow_id'=>$follow_id, 'user_id'=>$user_id);
		$res =  $this->add($map);
		if ($res)
		{
			$userService	= new BaseDbService('user');
			$userService->update($user_id, array('follow_num'=>'expression:follow_num+1'));
			$userService->update($follow_id, array('fans_num'=>'expression:fans_num+1'));
		}
		return true;
	}
	
	public function FollowUserRemove($user_id,$follow_id)
	{
		$followInfo	= $this->IsFollow($user_id, $follow_id);
		if (!$followInfo)
		{
			return true;
		}
		$res =  $this->del($followInfo['id']);
		if ($res)
		{
			$userService	= new BaseDbService('user');
			$userService->update($user_id, array('follow_num'=>'expression:follow_num-1'));
			$userService->update($follow_id, array('fans_num'=>'expression:fans_num-1'));
		}
		return true;
	}
	
	public function getFollowByUid($uid)
	{
		return $this->rows(array('user_id'=>'eq:'.$uid),'', array('follow_id'));
		
	}
	
}